package com.tang.calculator.utils;

import com.tang.calculator.context.CalculateContext;
import com.tang.calculator.symbols.Calculation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class CheckUtils {

    @Autowired
    List<Calculation> calculations;

    /**
     * 运算符检查器
     *
     * @param context
     * @param replace
     * @param str
     * @return
     */
    public String coordinate(CalculateContext context, String replace, String str) {
        if (replace.contains(str)) { //(1+sum(0,3)+1+2)*3
            int a = 0, i = 0;
            while (true) {
                i++;
                String[] strs = new String[2];
                replace = replace.substring(a);
                int anInt = getInt(replace, str, context.getCount());
                if (!"-".equals(str))
                    if (anInt == 0) return "第" + i + "个" + str + "号运算符使用错误";
                if (replace.length() - 1 <= anInt) return "第" + i + "个" + str + "号运算符使用错误";
                if (anInt == -1) return null;
                strs[0] = Upstream(replace, anInt);
                strs[1] = Downstream(replace, anInt);
                if (operator(strs)) return "第" + i + "个" + str + "号运算符使用错误";
                a = anInt + 1;
            }
        }
        return null;
    }

    /**
     * 确定指定字符坐标 没有返回-1
     *
     * @param string
     * @param getString
     * @return
     */
    public int getInt(String string, String getString, List<Integer> list) {
        if (string.contains(getString)) {
            for (int i = 0; i < string.length() - getString.length(); i++) {
                if (list.contains(i)) continue;
                int a = string.indexOf(getString, i);
                if (a >= 0) {
                    if (CollectionUtils.isEmpty(list)) {
                        list.add(a);
                    } else {
                        list.add(list.get(list.size() - 1) + a);
                    }
                    return a;
                }
            }
        }
        return -1;
    }

    /**
     * 字符串上游
     *
     * @param string
     * @param inder
     * @return
     */
    public String Upstream(String string, Integer inder) {
        if (inder == 0) return "0";
        return string.substring(inder - 1, inder);
    }

    /**
     * 字符串下游
     *
     * @param string
     * @param inder
     * @return
     */
    public String Downstream(String string, Integer inder) {
        String substring = string.substring(inder + 1, inder + 2);
        //过滤负数
        if ("-".equals(substring)) {
            substring = " ";
        }
        return substring;
    }

    /**
     * 校验符号
     *
     * @param strs
     * @return
     */
    public boolean operator(String[] strs) {
        List<String> collect = calculations.stream().map(li -> li.getName()).collect(Collectors.toList());
        if (collect.contains(strs[0]) || "(".equals(strs[0])) return true;
        if (collect.contains(strs[1]) || ")".equals(strs[1])) return true;
        return false;
    }

}



